package org.example.iris.controller;


import org.example.iris.dto.Result;
import org.example.iris.dto.StudentInfo;
import org.example.iris.mapper.StudentFileMapper;
import org.example.iris.entity.*;
import org.example.iris.service.StudentService;
import org.example.iris.service.UserService;
import org.example.iris.utils.Md5Util;
import org.example.iris.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private StudentFileMapper studentFileMapper;


    @PostMapping("register")
    public Result register(@RequestBody Map<String, String> params) {
        if (userService.findByUsername(params.get("username")) != null) {
            return Result.error("用户名已存在");
        }
        if(!params.get("password").equals(params.get("repassword"))){
            return Result.error("两次密码不一致");
        }
        User user = new User();
        user.setPhone(params.get("phoneNum"));
        user.setUsername(params.get("username"));
        user.setPassword(Md5Util.getMD5String(params.get("password")));
        user.setPhone(params.get("phoneNum"));
        user.setRole("student");

        Student student = new Student();

        studentService.register(user, student);
        return Result.success();
    }

    @GetMapping("/stuInfo")
    public Result<StudentInfo> stuInfo(@RequestParam(required = false) Integer userId) {

        Map<String, Object> claims = ThreadLocalUtil.get();
        if(userId == null) {
            userId = (Integer) claims.get("userId");
        }

        User user = userService.findById(userId);
        Student student = studentService.findByUserId(userId);

        StudentInfo studentInfo = studentService.getStuInfo(user, student);

        return Result.success(studentInfo);
    }

    @PutMapping("updateStuInfo")
    public Result updateStuInfo(@RequestBody StudentInfo studentInfo) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        Student student = new Student();

        student.setUserId(userId);
        student.setRealName(studentInfo.getRealName());
        student.setGender(studentInfo.getGender());
        student.setWechatNum(studentInfo.getWechatNum());
        student.setBirth(studentInfo.getBirth());

        studentService.updateStuInfo(student);
        return Result.success();
    }

    @PutMapping("updateStatus")
    public Result updateStatus(@RequestBody String status) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        studentService.updateStuStatus(userId, status);
        return Result.success();
    }

    @PutMapping("updateSkill")
    public Result updateSkill(@RequestBody String skills) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        studentService.updateStuSkill(userId, skills);
        return Result.success();
    }

    @PutMapping("updateDesiredJob")
    public Result updateDesiredJob(@RequestBody DesiredJob desiredJob) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        // 转换 preferCities 为字符串
        if (desiredJob.getWorkCity() != null) {
            desiredJob.setWorkCityString(String.join(",", desiredJob.getWorkCity()));
            System.out.println(desiredJob.getWorkCity());
            System.out.println(desiredJob.getWorkCityString());
        }
        if (desiredJob.getSalaryRange() != null) {
            desiredJob.setSalaryRangeString(String.join(",", desiredJob.getSalaryRange()));
            System.out.println(desiredJob.getWorkCity());
            System.out.println(desiredJob.getWorkCityString());
        }


        studentService.updateStuDesiredJob(userId, desiredJob);
        return Result.success();
    }

    @PutMapping("updateWorkExperience")
    public Result updateWorkExperience(@RequestBody WorkExperience workExperience) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        if (workExperience.getJobTimeRange() != null) {
            workExperience.setJobTimeRangeString(String.join(",", workExperience.getJobTimeRange()));
            System.out.println(workExperience.getJobTimeRange());
            System.out.println(workExperience.getJobTimeRangeString());
        }

        studentService.updateStuWorkExperience(userId, workExperience);
        return Result.success();
    }

    @PutMapping("updateProjectExperience")
    public Result updateProjectExperience(@RequestBody ProjectExperience projectExperience) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        if (projectExperience.getProjectTimeRange() != null) {
            projectExperience.setProjectTimeRangeString(String.join(",", projectExperience.getProjectTimeRange()));
            System.out.println(projectExperience.getProjectTimeRange());
            System.out.println(projectExperience.getProjectTimeRangeString());
        }

        studentService.updateStuProjectExperience(userId, projectExperience);
        return Result.success();
    }

    @PutMapping("updateEducationExperience")
    public Result updateEducationExperience(@RequestBody EducationExperience educationExperience) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        if (educationExperience.getSchoolTimeRange() != null) {
            educationExperience.setSchoolTimeRangeString(String.join(",", educationExperience.getSchoolTimeRange()));
            System.out.println(educationExperience.getSchoolTimeRange());
            System.out.println(educationExperience.getSchoolTimeRangeString());
        }

        studentService.updateStuEducationExperience(userId, educationExperience);
        return Result.success();
    }

    @PutMapping("addDesiredJob")
    public Result addDesiredJob(@RequestBody DesiredJob desiredJob) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        // 转换 preferCities 为字符串
        if (desiredJob.getWorkCity() != null) {
            desiredJob.setWorkCityString(String.join(",", desiredJob.getWorkCity()));
            System.out.println(desiredJob.getWorkCity());
            System.out.println(desiredJob.getWorkCityString());
        }
        if (desiredJob.getSalaryRange() != null) {
            desiredJob.setSalaryRangeString(String.join(",", desiredJob.getSalaryRange()));
            System.out.println(desiredJob.getWorkCity());
            System.out.println(desiredJob.getWorkCityString());
        }

        studentService.addStuDesiredJob(userId, desiredJob);
        return Result.success();
    }

    @PutMapping("addEducationExperience")
    public Result addEducationExperience(@RequestBody EducationExperience educationExperience) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        if (educationExperience.getSchoolTimeRange() != null) {
            educationExperience.setSchoolTimeRangeString(String.join(",", educationExperience.getSchoolTimeRange()));
            System.out.println(educationExperience.getSchoolTimeRange());
            System.out.println(educationExperience.getSchoolTimeRangeString());
        }

        studentService.addStuEducationExperience(userId, educationExperience);
        return Result.success();
    }

    @PutMapping("addWorkExperience")
    public Result addWorkExperience(@RequestBody WorkExperience workExperience) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        if (workExperience.getJobTimeRange() != null) {
            workExperience.setJobTimeRangeString(String.join(",", workExperience.getJobTimeRange()));
            System.out.println(workExperience.getJobTimeRange());
            System.out.println(workExperience.getJobTimeRangeString());
        }

        studentService.addStuWorkExperience(userId, workExperience);
        return Result.success();
    }

    @PutMapping("addProjectExperience")
    public Result addProjectExperience(@RequestBody ProjectExperience projectExperience) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        if (projectExperience.getProjectTimeRange() != null) {
            projectExperience.setProjectTimeRangeString(String.join(",", projectExperience.getProjectTimeRange()));
            System.out.println(projectExperience.getProjectTimeRange());
            System.out.println(projectExperience.getProjectTimeRangeString());
        }

        studentService.addStuProjectExperience(userId, projectExperience);
        return Result.success();
    }

    @DeleteMapping("deleteDesiredJob")
    public Result deleteDesiredJob(@RequestBody Map<String,Integer> params) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        studentService.deleteDesiredJob(userId, params.get("desiredJobId"));
        return Result.success();
    }

    @DeleteMapping("deleteEducationExperience")
    public Result deleteEducationExperience(@RequestBody Map<String,Integer> params) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        studentService.deleteEducationExperience(userId, params.get("educationExperienceId"));
        return Result.success();
    }

    @DeleteMapping("deleteWorkExperience")
    public Result deleteWorkExperience(@RequestBody Map<String,Integer> params) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        studentService.deleteWorkExperience(userId, params.get("workExperienceId"));
        return Result.success();
    }

    @DeleteMapping("deleteProjectExperience")
    public Result deleteProjectExperience(@RequestBody Map<String,Integer> params) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");

        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        studentService.deleteProjectExperience(userId, params.get("projectExperienceId"));
        return Result.success();
    }


    @PostMapping("updatePicture")
    public Result updatePicture(MultipartFile file) throws Exception {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        String url = studentService.updateStuPicture(file, userId);
        return Result.success(url);
    }



}
